const collect = require('../../models/collect')
const house = require('../../models/houses')

//用户收藏哪些房源
exports.getUsercollectId = async function (userId) {

    const result = await collect.findAll({ //查询配置，查询条件
        where: {
            userId
        },
        attributes: ['houseId'],
        raw: true
    }).catch(()=>{})
    // console.log(result.map(item=>item.couponId));
    if (result != '') return {
        data: result.map(item => item.houseId),
        meta: {
            msg: '查询成功',
            status: '200'
        }
    }
    return {
        data: {},
        meta: {
            msg: '暂无数据或用户不存在',
            status: '404'
        }
    }
}

// 查询房源详情
exports.getUsercollect = async function (houseId) {
    const result = await house.findAll({ //查询配置，查询条件
        where: {
            id: houseId
        },
        raw: true
    }).catch(()=>{})
    console.log(result);
    if (result != '') return {
        data: result,
        meta: {
            msg: '获取收藏房源成功',
            status: '200'
        }
    }
    return {
        data: '',
        meta: {
            msg: '房源id不存在',
            status: '404'
        }
    }
}



exports.DeleteUsercollect = async function (userId,houseId) {
    Coupon_user.destroy({
        where: {
            userId,
            houseId
        }
    }).catch(()=>{})
}

exports.addUsercollect = async function (userId,houseId) {
    Coupon_user.create({
        userId,
        houseId
    }).catch(()=>{})
    return '1'
}













//goods
// 查询用户是否收藏该goods
exports.getWhetherUserCollectGoods = async function (userId,goodsId) {
    console.log(userId,goodsId);
    const result = await collect.findAll({ //查询配置，查询条件
        where: {
            userId,
            goodsId
        },
        raw: true
    }).catch(()=>{})
    console.log(result);
    // console.log(result.map(item=>item.couponId));
    if(result != '') return true
    return false
}


exports.userCollectGoods = async function (userId,goodsId) {
    collect.create({
        userId,
        goodsId
    }).catch(()=>{})
    return true
}


exports.userDeletecollect = async function (userId,goodsId) {
    console.log('delete');
    console.log(userId,goodsId);
    collect.destroy({
        where: {
            userId:userId,
            goodsId:goodsId
        }
    }).catch(()=>{})
    return true
}
